﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;

namespace StockMatch.Service.AOPBasics
{
    public abstract class AOPAspect : IMessageSink
    {
        private IMessageSink _NextSink;

        private string _AOPPropertyName;

        public string AOPPropertyName
        {
            get { return _AOPPropertyName; }
            set { _AOPPropertyName = value; }
        }
        public AOPAspect(IMessageSink nextsink)
        {
            _NextSink = nextsink;
        }

        public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
        {
            return null;
        }

        public IMessageSink NextSink
        {
            get { return _NextSink; }
        }

        #region old
        //public IMessage SyncProcessMessage(IMessage msg)
        //{
        //    IMethodCallMessage callmsg = msg as IMethodCallMessage;
        //    if (callmsg == null)
        //    {
        //        return null;
        //    }
        //    IMessage resultMsg = null;
        //    BeforeAchieve();
        //    resultMsg = _NextSink.SyncProcessMessage(msg);
        //    AfterAchieve();
        //    return resultMsg;
        //}

        //protected virtual void BeforeAchieve()
        //{

        //}

        //protected virtual void AfterAchieve()
        //{

        //}

        #endregion

        #region new

        public IMessage SyncProcessMessage(IMessage msg)
        {
            IMethodCallMessage callmsg = msg as IMethodCallMessage;
            if (callmsg == null)
            {
                return null;
            }
            IMessage resultMsg = null;
            ConfigRelationExecution.ActionConfigByAspectName(_AOPPropertyName, callmsg.MethodName,callmsg.TypeName,  InterceptAchieveType.Before, callmsg);
            resultMsg = _NextSink.SyncProcessMessage(msg);
            ConfigRelationExecution.ActionConfigByAspectName(_AOPPropertyName, callmsg.MethodName, callmsg.TypeName, InterceptAchieveType.After, resultMsg);
            return resultMsg;
        }

        #endregion
    }
}
